home *** CD-ROM | disk | FTP | other *** search
- 10 'ERRORS1.BAS - Demonstrating how YOU can trap errors in a BASIC program
- 20 'From the GW-BASIC Tutorial Series, GWBT07, 12/15/1990
- 30 '
- 40 'Variables used in this program:
- 50 '
- 60 ' ERR - provided by BASIC to hold the error code
- 70 ' ERL - provided by BASIC to hold the error line number
- 80 '
- 90 'Start of main program:
- 100 ON ERROR GOTO 1000
- 110 CLS:KEY OFF
- 120 PRINT "I am going to show you how you can prevent errors from happening in"
- 130 PRINT "your BASIC programs."
- 140 PRINT
- 150 PRINT "The first error we're going to demonstrate is what happens when you"
- 160 PRINT "leave a disk drive door open. Please take any disk out of your A:"
- 170 PRINT "drive, and press a key when you have done this..."
- 180 'Pause until user presses a key...
- 190 WHILE INKEY$="":WEND
- 200 FILES "A:\"
- 210 PRINT "You should have seen a message that there was no diskette in your A:"
- 220 PRINT "drive. Now that you've seen this, let's see what happens when we"
- 230 PRINT "try to read a file that is not on your diskette."
- 240 PRINT "Press a key when there is a diskette in your A: drive..."
- 250 WHILE INKEY$="":WEND
- 260 OPEN "A:\DATA.DAT" FOR INPUT AS #1
- 270 PRINT "You should have seen the message that a needed file could not be "
- 280 PRINT "found."
- 290 PRINT "Next, let's look at what happens when you try to use a printer that"
- 300 PRINT "is not ready. If your printer is ON, please turn it OFF."
- 310 PRINT "Press any key when your printer is OFF..."
- 320 'Again, wait for a keypress...
- 330 WHILE INKEY$="":WEND
- 340 LPRINT "Hello there!"
- 350 PRINT "You'll note that this time, the program did NOT continue, but waited "
- 360 PRINT "until you had fixed the printer and it was ready to print our message."
- 370 PRINT
- 380 PRINT "If you examine the listing of the error handling routine (lines 1000"
- 390 PRINT "and up), you'll see the difference."
- 400 PRINT "The program is now ending. Thanks for your cooperation!"
- 410 'NOTE that you must have an END statement before your error handling routine
- 420 'starts, otherwise BASIC will happily run into it, and cause you many
- 430 'headaches later in life!
- 440 END
- 1000 'Beginning of error handling routine. BASIC will give us the ERR variable
- 1010 'containing the Error Code, and ERL containing the line number where the
- 1020 'error happened. In this demonstration, we don't need to use the ERL
- 1030 'value, but the ERR will come in handy...
- 1040 '
- 1050 'The error code for a printer error, depending on your printer/port, could
- 1060 'be either 24 (Device Timeout), 25 (Device Fault) or 27 (Out of Paper), so
- 1070 'let's set our first 'trap' to catch these...
- 1080 IF ERR=24 OR ERR=25 OR ERR=27 THEN BEEP:PRINT:PRINT "Printer is not ready! Press a key to continue...":PRINT:WHILE INKEY$="":WEND:RESUME
- 1090 'The RESUME statement tells BASIC to go back to the line that the error happened in, and try it again (hopefully the error has been corrected)
- 1100 '
- 1110 'The error code for a disk drive not ready is 71 (Disk not Ready). Let's
- 1120 'stop it from happening...
- 1130 IF ERR=71 THEN BEEP:PRINT:PRINT "Disk drive A: is not ready! Continuing with program...":PRINT:RESUME NEXT
- 1140 '
- 1150 'Notice the RESUME NEXT - this tells BASIC to go to the next line in the
- 1160 'program to continue after the error.
- 1170 '
- 1180 'The error for a file not found is 53 (File not Found). Let's stop this
- 1190 'error from happening...
- 1200 IF ERR=53 THEN BEEP:PRINT:PRINT "Requested file was not found on the diskette! Continuing with program...":PRINT:RESUME 290
- 1210 'Note that in this case we used RESUME with a line number. You CAN do this
- 1220 'if you know exactly where to go if an error happens.
- 1230 '
- 1240 'The final lines are for any errors that we didn't trap...
- 1250 ON ERROR GOTO 0
- 1260 'If an error happens that we didn't set up a trap for, it will print the
- 1270 'error message and end the program.
- 1280 '
- 1290 'End of error handling section
- 1300 '
- 1310 'End of program - ERRORS1.BAS
-